<?

// ********************************
//	extracts site struct for viewing
//	v1.02.1 2011.09.18 
// changes:
// * page id extraction fix
// * pagenames fetch optimization
// ********************************

require_once E.'_lib/_const.php';

class STRUCTMAP {

	var $parent;
	var $path;
	var $lang;
	var $data;
	
	var $res;
	
	function STRUCTMAP() {

		$this->data = array();
		$this->res = 255;

	}
	
	function init($params = array()) {
	
		$this->params = array_merge( array('parent' => 0, 'path' => '', 'lang' => context('lang'), 'hook' => 'structmap', 'bit' => PT_CONFIGURABLE, 'attr' => 0), (array)$params);
		
		$this->names = datafetch_array('db_list', array('table' => tb('data'), 
			'where' => "(id_data=".D_PAGENAMES.") AND	((assoc IS NULL) OR (assoc='') OR INSTR(assoc,'".$this->params['lang']."')!=0) AND (name='name')",
			'index' => 'id_parent'
		));

		$this->res = $this->fetch();
		return ! (bool) $this->res;
	}
	
	function fetch() {
	
		return ($this->data = $this->_Structmap($this->params['parent'], $this->params['path'], $this->params['lang'], $this->params['hook'], $this->params['bit'], $this->params['attr'])) ? 0 : 1;
		
	}



	function _Structmap($parent, $path, $lang, $hook, $bit, $attr) {
	
		$res = array();
	
	//	extract records that:
	//	1. have this parent
	//	2. are visible in Sitemap
	//	3. are accessible by current user, i.e.
	//		PT_ISUSER bit is 0 for ANONYMOUS, 0 or 1 for USER

		if (($pages = dataset('db_list', array(
							'table' => tb('struct'),
							'index' => 'id',
							'order' => 'pos',
							'where' => "(parent=$parent) AND
								(".dbAndS('tag', $bit).") AND 
								(".dbAndS('attr', $attr).") AND 
								( (".dbAndI('tag', PT_ISUSER).") OR ".(user_info('priv',0) & PT_ISUSER)."!=0)") )) && sizeof($pages->data)) {
	
	
			foreach($pages->data as $id => $pg) {
			
				// if non-def lang or empty page name - extract page name from table 'list'
//				if ($nm = datafetch('meta', D_PAGENAMES, $id, $lang, 'name'))
				if ($nm = $this->names[$id]['value'])
					$pg['name'] = $nm;
	
				// check page for hook 'map' to determine it's URL

				if ($code = code($hook, $pg['code'])) {	
					if (is_file("_code/_{$code}_{$hook}.php")) {
						$p = include "_code/_{$code}_{$hook}.php";				// outer code executed

						if (is_string($p)) {								// and returned string - a part of path
							if ($p)
								$pg['url'] = $path.$p.'/';
							else											// if returned empty string
								$pg['url'] = $path;
						} 													// !!! or it returned something else - need 2b checked
					}
				} else {
					$p = $pg['keyword'];										// no support for multiple keywords in one record
																				// due to single Page Name record per Page Id
					if ($p)
					$pg['url'] = ($path) ? $path.$p.'/' : '/'.$p;
					else											// if returned empty string
						$pg['url'] = $path;
					
				}
				
				$temp = $pg;


				if ($l = $this->_Structmap($id, $pg['url'], $lang, $hook, $bit, $attr)) 	// if found siblings - link them to 'data'
					$temp['data'] = $l;

				
				if ($code = code('sitemap', $pg['code'])) {				// execute hook "generated"
					if (is_file("_code/_{$code}_sitemap.php")) {			// hook must return an array structured like 'data'
						$p = include "_code/_{$code}_sitemap.php";		// and containing links for this page not defined in struct 
																			// but generated some another way
						if ($p)
							$temp['data'] = array_merge((array)$temp['data'], (array)$p);
					}
				}


//				if (($pg['kind'] == PK_REDIRECT) && (is_array($temp['data'])))	// if not real page but redirect - show siblings
//					$temp = array_shift($temp['data']);									// usually redirect have ONE sibling	

	
				$res[$temp['id']] = $temp;													// another brick in the wall
			}

		}
		
		return (sizeof($res)) ? array_mark_bounds($res) : false;
	}


}